package com.org.oracle.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.org.oracle.mongodb.entity.*;
import com.org.oracle.service.CircleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * @author suze
 * @date 2024/8/18
 * @time 20:36
 **/
@Service
@Slf4j
@Primary
public class CircleServiceImpl implements CircleService {

    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public List<Circle> find_all() {
    // 创建查询对象
        Query query = new Query();
    // 执行查询
        List<Circle> results = mongoTemplate.find(query, Circle.class);
        return results;
    }
    @Override
    public List<Circle> find_by_tag(String tag) {
        Query query = new Query();
        query.addCriteria(Criteria.where("tag").is(tag));
        return mongoTemplate.find(query, Circle.class);
    }

    @Override
    public List<Circle> find_by_word(String word) {
        Query query = new Query();
        query.addCriteria(Criteria.where("tag").is(word).
                orOperator(Criteria.where("introduce").is(word)).
                orOperator(Criteria.where("circleName").is(word)));
        return mongoTemplate.find(query, Circle.class);
    }

    @Override
    public List<Circle> find_by_user(String uid) {
        //这里要查另外一个表  members的表
        Query query = new Query();
        query.addCriteria(Criteria.where("userid").is(uid));

        Query query2 = new Query();
        List<Member> list = mongoTemplate.find(query, Member.class);
        List<Circle> results = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).getCircleId();
            query2.addCriteria(Criteria.where("circleId").is(list.get(i).getCircleId()));
            results.add(mongoTemplate.findOne(query2,Circle.class));
        }
        return results;
    }

    @Override
    public Object join_circle(String uid, String cid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userid").is(uid));
        User user = mongoTemplate.findOne(query, User.class);
        if (user != null) {
            Query query2 = new Query();
            query2.addCriteria(Criteria.where("circleId").is(cid));
            Circle circle = mongoTemplate.findOne(query2, Circle.class);
            Member member = new Member();
            member.setCircleId(cid);
            member.setName(user.getName());
            member.setAvatar(user.getAvatar());
            member.setUserid(user.getUid());
            member.setId(RandomUtil.randomNumbers(15));
            mongoTemplate.save(member);
            //还要给用户的任务表进行初始化  根据用户从哪个话题ai进来的选择对应的章节任务


            return "加入成功";
        }
        return "null";
    }

    @Override
    public CircleContext send_note(String uid, CircleContext circleContext) {
        //id必须有且不能重复  这里前端应该可以生成 按照时间戳生成uUid
        //circleContext.setid(RandomUtil.randomNumbers(20));
        mongoTemplate.insert(circleContext,"CircleContext_"+circleContext.getCircleId());
        return circleContext;
    }

    @Override
    public Comment send_comment(String uid, Comment comment) {
        mongoTemplate.insert(comment);
        return comment;
    }

    @Override
    public List<Comment> find_comment(String cmid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("cmid").is(cmid));
        if (mongoTemplate.findOne(query,Comment.class) != null) {
            return mongoTemplate.find(query,Comment.class);
        }
        return null;
    }

    @Override
    public Comment reply_comment(String uid, Comment comment) {
        Query query = new Query();
        //找到用户想要回复的那个人的评论
        query.addCriteria(Criteria.where("uuid").is(comment.getReplyId()));
        Comment uComment = mongoTemplate.findOne(query, Comment.class);
        if (uComment != null) {
            List<Comment> subComment = uComment.getSubComment();
            subComment.add(comment);
            uComment.setSubComment(subComment);
            mongoTemplate.save(uComment);
            return uComment;
        }
        return null;
    }
}
